package scc.project.entity.vo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 常规的翻页Vo对象
 *
 * @author 陈圳佳
 *         日期：2015/6/22
 */
public class PageVo<E> {
    /**
     * 页码
     */
    private int pageNum;
    /**
     * 总数
     */
    private long total;
    /**
     * 偏移
     */
    private int offset;
    /**
     * 数量
     */
    private int rows = 10;
   /* */
    /**
     * 数据
     */
    private List<E> data;
    /**
     * 参数
     */
    private Map<String, Object> params;

    private Map<String, String> properties;

    public Map<String, String> getProperties() {
        return properties;
    }

    public PageVo<E> setProperties(Map<String, String> properties) {
        this.properties = properties;
        return this;
    }

    public PageVo<E> addProperty(String key, String value) {
        properties = properties == null ? new HashMap<String, String>() : properties;
        properties.put(key, value);
        return this;
    }

    public String getProperty() {
        if (properties == null) return "";
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            sb.append(" ").append(entry.getKey()).append("=").append(entry.getValue()).append(" ");
        }
        return sb.toString();
    }

    public PageVo<E> addExclude(String ex) {
        params.remove(ex);
        return this;
    }

    public PageVo(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageNum() {
        if (this.pageNum <= 0) {
            this.pageNum = 1;
            return 1;
        } else {
            return pageNum;
        }
    }

    public PageVo<E> setPageNum(int pageNum) {
        this.pageNum = pageNum;
        return this;
    }

    public long getTotalPage() {
        return ((this.getTotal() - 1) / this.getRows()) + 1;
    }

    public int getOffset() {
        this.offset = (this.getPageNum() - 1) * this.getRows();
        return offset;
    }

    public PageVo<E> setOffset(int offset) {
        this.offset = offset;
        return this;
    }

    public int getRows() {
        return rows;
    }

    public PageVo<E> setRows(int rows) {
        this.rows = rows;
        return this;
    }

    protected String joinParams() {
        StringBuilder sb = new StringBuilder();
        if (params == null)
            return sb.toString();
//        sb.append("?");
        boolean flag = false;
        for (String key : params.keySet()) {
            Object value = params.get(key);
            if (value != null) {
                flag = true;
                sb.append(key).append("=").append(value).append("&");
            }
        }
        return flag ? "?" + sb.toString() : sb.toString();
    }

    public String getUrl(long num) {
        StringBuilder sb = new StringBuilder();
        String joinParams = joinParams();
        sb.append(joinParams).append("p=").append(num);
        return sb.toString();
    }

    public String getPageNumHtml() {
        StringBuilder sb = new StringBuilder();
        sb.append("<ul>");
        // 首页，上一页
        if (this.getPageNum() != 1) {
            sb.append("<li><a href='").append(this.getUrl(1)).append("' title='首页'").append(getProperty()).append(">首页</a></li>");
            sb.append("<li><a href='").append(this.getUrl(this.getPageNum() - 1)).append("' title='上一页'").append(getProperty()).append(">上一页</a></li>");
        }
        // 页码
        if (this.getTotalPage() != 1) {
            int startNum = this.getPageNum() - 3 <= 1 ? 1
                    : this.getPageNum() - 3;
            long endNum = this.getPageNum() + 3 >= this.getTotalPage() ? this
                    .getTotalPage() : this.getPageNum() + 3;
            if (startNum > 1) {
                sb.append("<li><a href='javascript:void(0);'>...</a></li>");
            }
            for (int i = startNum; i <= endNum; i++) {
                if (i == pageNum) {
                    sb.append("<li class='active'><a class='current' title='").append(i).append("'>").append(i).append("</a></li>");
                } else {
                    sb.append("<li><a href='").append(this.getUrl(i)).append("' class='number' title='").append(i).append("'").append(getProperty()).append(">").append(i).append("</a></li>");
                }
            }
            if (endNum < this.getTotalPage()) {
                sb.append("<li><a href='javascript:void(0);'>...</a></li>");
            }
        }
        // 下一页，尾页
        if (this.getPageNum() < this.getTotalPage()) {
            sb.append("<li><a href='").append(this.getUrl(this.getPageNum() + 1)).append("' title='下一页'").append(getProperty()).append(">下一页</a></li>");
            sb.append("<li><a href='").append(this.getUrl(this.getTotalPage())).append("' title='末页'").append(getProperty()).append(">末页</a></li>");
        }
        sb.append("</ul>");
        return sb.toString();
    }

    public long getTotal() {
        return total;
    }

    public PageVo<E> setTotal(long total) {
        this.total = total;
        return this;
    }

    public PageVo<E> setParams(Map<String, Object> params) {
        this.params = params;
        return this;
    }

    public PageVo<E> addParam(String key, Object value) {
        this.params = params == null ? new HashMap<String, Object>(0) : params;
        params.put(key, value);
        return this;
    }

    public List<E> getData() {
        return data;
    }

    public PageVo<E> setData(List<E> data) {
        this.data = data;
        return this;
    }

    public Map<String, Object> getParams() {
        return params;
    }

}
